/* 
 *
 * Persistence4J - Simple library for data persistence using java
 * Copyright (c) 2010, Avdhesh yadav.
 * http://www.avdheshyadav.com
 * Contact: avdhesh.yadav@gmail.com
 *
 * This library is free software; you can redistribute it and/or modify it under the terms
 * of the GNU Lesser General Public License as published by the Free Software Foundation,
 * either version 3 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this
 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 */

package com.avdy.p4j.jdbc.dao;


import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.avdheshyadav.dbcrawler.dbmodel.Column;
import com.avdy.p4j.jdbc.common.JdbcSqlUtil;
import com.avdy.p4j.jdbc.dbms.metadata.TableStructure;
import com.avdy.p4j.jdbc.service.GenericDAO;
import com.avdy.p4j.jdbc.service.GenericDTO;

/**
 * 
 * @author Avdhesh Yadav
 *
 */
public class GenericDTORowProcessor implements RowProcessor
{
	/** */
	private TableStructure mTableStructure;
	/** */
	private int mFetchSize;

	/**
	 * 
	 * @param tableStructure
	 */
	public GenericDTORowProcessor(TableStructure tableStructure)
	{
		this(tableStructure, 10);
	}
	/**
	 * 
	 * @param tableStructure TableStructure
	 */
	public GenericDTORowProcessor(TableStructure tableStructure , int fetchSize)
	{
		this.mTableStructure = tableStructure;
		mFetchSize = fetchSize;
	}
	
	/**
	 * 
	 */
	public List<GenericDTO> toBeanList(ResultSet rs, int startPosition, int rows) throws SQLException
	{
		setResultSetAttribute(rs, startPosition);
		if(rows != GenericDAO.ALL_ROWS)
		{
			rows = startPosition + rows -1;
		}
		try 
		{
			List<GenericDTO> retVals = (List<GenericDTO>)copyResultSetToEntityObject(mTableStructure, rs, rows);
			return retVals;
		} 
		catch (Exception e) 
		{
			throw new SQLException(e);
		}
	}
	
	/**
	 * 
	 * @param resultSet ResultSet
	 * @param startPosition int
	 * 
	 * @throws SQLException
	 */
	private void setResultSetAttribute(ResultSet resultSet, int startPosition)throws SQLException
	{
		resultSet.setFetchSize(mFetchSize);
		resultSet.relative(startPosition - 1);
	}
	
	
	/**
	 * 
	 * @param tableStructure TableStructure
	 * @param resultSet ResultSet
	 * @param rows int
	 * 
	 * @return List<GenericDTO>
	 * 
	 * @throws SQLException
	 * @throws Exception
	 */
	private List<GenericDTO> copyResultSetToEntityObject(TableStructure tableStructure, ResultSet resultSet,int rows) throws SQLException , Exception
	{
		List<GenericDTO> ll = new ArrayList<GenericDTO>();
		List<Column> coList = tableStructure.getColumns();
		
		GenericDTO gvo = null;
		
		while(resultSet != null && resultSet.next())
		{
			Map<String, Object> data = new HashMap<String, Object>();
			for(Column column : coList)
			{
				String columnName = column.getColumnName().toLowerCase();
				int columnType = column.getDataType().getType();
				Object obj = JdbcSqlUtil.sqlToJava(resultSet, columnType, columnName);
				data.put(columnName, obj);
			}
			gvo = new GenericDTO(tableStructure.getFullTableName(),data);
			ll.add(gvo);
			
			if (resultSet.getRow() == (rows))
			{
				break;
			}
		}
		return ll;
	}
}